package com.example.toolproject.demo.controller;

import com.example.toolproject.demo.domain.Answer;
import com.example.toolproject.demo.domain.Question;
import com.example.toolproject.demo.domain.User;
import com.example.toolproject.demo.service.QuestionService;
import com.example.toolproject.demo.util.ResultBean;
import com.example.toolproject.demo.util.jwt.JwtUtil;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName QuestionController
 * @Description TODO
 * @Author lvyongwei
 * @Date 2020-10-26 10:14
 **/
@Controller
@RequestMapping("/question")
@CrossOrigin
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    /**
     * 用户添加问题
     * @param question
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addQuestion",method = RequestMethod.POST)
    public ResultBean addQuestion(@RequestBody Question question, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ResultBean.error("token无效");
        }
        return questionService.addQuestion(question,u.getId());
    }


    /**
     * 用户添加评论
     * @param answer
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addAnswer",method = RequestMethod.POST)
    public ResultBean addAnswer(@RequestBody Answer answer, HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        System.err.println(u);
        if(u == null){
            return ResultBean.error("token无效");
        }
        return questionService.addAnswer(answer,u.getId());
    }


    /**
     * 用户删除自己的评论(管理员删除任何人的评论)
     * @param data
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteAnswer",method = RequestMethod.POST)
    public ResultBean deleteAnswer(@RequestBody String data,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ResultBean.error("token无效");
        }
        JSONObject jsonObject=new JSONObject(data);
        Integer answerId=jsonObject.getInt("answerId");
        return questionService.deleteAnswer(answerId,u);
    }


    /**
     * 管理员删除用户提的问题;用户删除自己提的问题
     * @param data
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteQuestion",method = RequestMethod.POST)
    public ResultBean deleteQuestion(@RequestBody String data,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String jwt = token.substring(token.lastIndexOf(" ")+1);
        User u = JwtUtil.unsign(jwt,User.class);
        if(u == null){
            return ResultBean.error("token无效");
        }
        JSONObject jsonObject=new JSONObject(data);
        Integer questionId=jsonObject.getInt("questionId");
        return questionService.deleteQuestion(questionId);
    }


    /**
     * 获取到评论信息
     * @param data
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getAnswer",method = RequestMethod.POST)
    public ResultBean getAnswer(@RequestBody String data){
        JSONObject jsonObject=new JSONObject(data);
        Integer questionId=jsonObject.getInt("questionId");
        return questionService.getAnswer(questionId);
    }


    /**
     * 展示用户填写的问题
     * @param data
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getQuestion",method = RequestMethod.POST)
    public ResultBean getQuestion(@RequestBody String data){
        JSONObject jsonObject=new JSONObject(data);
        Integer pageNum=jsonObject.getInt("pageNum");
        Integer pageSize=jsonObject.getInt("pageSize");
        String username=jsonObject.getString("username");
        String school=jsonObject.getString("school");
        String question=jsonObject.getString("question");
        return questionService.getQuestion(pageNum,pageSize,username,school,question);
    }

}
